<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Basket_model extends CI_Model {
	function show_block() {
		$basket = $this->get_basket_list(true);
		if ($basket['count'] > 0) {

			switch ($basket['count']) {
				case "1": $ended = ""; break;
				case "2": $ended = "а"; break;
				case "3": $ended = "а"; break;
				case "4": $ended = "а"; break;
				case "5": $ended = "ов"; break;
				case "6": $ended = "ов"; break;
				case "7": $ended = "ов"; break;
				case "8": $ended = "ов"; break;
				case "9": $ended = "ов"; break;
				default: $ended = "ов"; break;
			}
			$text = "<div><a class='basket-h' href='/basket/'>Корзина</a></div>";
			$text .= "<div style='margin-top: 7px;'><small>".$basket['count']." товар".$ended."</small></div>";
			$text .= "<div>Сумма к оплате ".tonum($basket['sum2'])." руб.</div>";
			$text .= "<div style='margin-top: 5px;'><a class='do_order' href='/basket/'>Оформить заказ</a></div>";
			return $text;
		} else {
			return "Ваша корзина<br /> пуста";
		}
	}
	function small_basket_block($div = true) {
		$basket = $this->get_basket_list(true);
		if ($basket['count'] > 0) {

			switch ($basket['count']) {
				case "1": $ended = ""; break;
				case "2": $ended = "а"; break;
				case "3": $ended = "а"; break;
				case "4": $ended = "а"; break;
				case "5": $ended = "ов"; break;
				case "6": $ended = "ов"; break;
				case "7": $ended = "ов"; break;
				case "8": $ended = "ов"; break;
				case "9": $ended = "ов"; break;
				default: $ended = "ов"; break;
			}
			$text = "";
			if ($div) $text .= "<div class='compare_block'>";
				$text .= "<div class='compare_wrapper'>";
					$text .= "<p><a href='/basket/'>В корзине</a>: ".$basket['count']." товар".$ended."</p>";
				$text .= "</div>";
			if ($div) $text .= "</div>";
			return $text;
		} else {
			return $this->check_compare();
		}
	}


	function get_basket_list($sum = false) {
		if ($this->session->userdata('basket_session') == "") {
			$this->session->set_userdata('basket_session', sha1(md5(md5(time()."basketdata"))));
		}
		$db = $this->db->select()->from('basket')->where('basket_session', $this->session->userdata('basket_session'))->get()->row_array();
		if (sizeof($db) > 0) {
			if ($sum == false)
				return json_decode($db['basket_info'], true);
			else {
				$basket = json_decode($db['basket_info'], true);
				$count = 0;
				foreach ($basket as $key=>$item) {
					$count = $count +  $item['count'];
					$item_info = $this->db->select('item_have, item_code')->from('catalog_items')->where('item_id',$key)->limit(1)->get()->row_array();
					$basket[$key]['have'] = $item_info['item_have'];
					$basket[$key]['item_code'] = $item_info['item_code'];
				}
				return array('basket' => $basket, 'count' => $count, 'sum' => $db['basket_price'], 'sum2' => $db['basket_price2']);
			}
		} else {
			$in_data['basket_id'] = 0;
			$in_data['basket_session'] = $this->session->userdata('basket_session');
			$in_data['basket_info'] = json_encode(array());
			$in_data['basket_price'] = 0.00;
			$in_data['basket_price2'] = 0.00;
			$this->db->insert('basket', $in_data);
			return array();
		}
	}
	function update_basket($basket, $type=null) {
		$all = 0; $all2 = 0; $all_price = 0; $all_price2 = 0;
		foreach ($basket as $item) {
			$all += $item['sum'];
			$all2 += $item['sum2'];
		}
		$this->db->where('basket_session', $this->session->userdata('basket_session'))->update('basket', array(
			'basket_info' => json_encode($basket),
			'basket_price' => floatval($all), // общая сумма за товары без предоплаты
			'basket_price2' => floatval($all2) // общая сумма за товары по предоплате
		));
	}
	function erase_basket() {
		$this->db->where('basket_session', $this->session->userdata('basket_session'))->delete('basket');
	}
	function new_orders() {
		return $this->db->select()->from('orders')->where('order_status', '1')->where('order_userdata != ', '')->count_all_results();
	}
	function parse_prices($string) {
		return json_decode($string, true);
	}
	function check_compare() {
		$data['list'] = json_decode(get_cookie('compare'), true);
		return $this->load->view('basket/compare_block', $data, true);
	}
}
?>